package com.luoxue.execsql.rest.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luoxue.execsql.common.contact.ExecSqlContact;
import com.luoxue.execsql.common.util.ResultBody;
import com.luoxue.execsql.rest.bean.base.controller.BaseController;
import com.luoxue.execsql.rest.entity.CommonValidator;
import com.luoxue.execsql.rest.entity.SqlSnippet;
import com.luoxue.execsql.rest.entity.UserTest;
import com.luoxue.execsql.rest.mapper.CommonValidatorMapper;
import com.luoxue.execsql.rest.mapper.UserTestMapper;
import com.luoxue.execsql.rest.service.ICommonValidatorService;
import com.luoxue.execsql.rest.service.IUserTestService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/commonValidator")
public class CommonValidatorController extends BaseController<CommonValidator, CommonValidatorMapper, ICommonValidatorService> {

    public CommonValidatorController(ICommonValidatorService service) {
        super(service);
    }

    @Override
    protected void queryParams(CommonValidator entity, QueryWrapper<CommonValidator> queryWrapper) {
        if (Strings.isNotBlank(entity.getRuleName())) {
            if (entity.getRuleName().startsWith(ExecSqlContact.refineSearch)) {
                queryWrapper.lambda().eq(CommonValidator::getRuleName, entity.getRuleName().substring(1));
            } else {
                queryWrapper.lambda().like(CommonValidator::getRuleName, entity.getRuleName());
            }
        }
        if (Strings.isNotBlank(entity.getRegexType())) {
            if (entity.getRegexType().startsWith(ExecSqlContact.refineSearch)) {
                queryWrapper.lambda().eq(CommonValidator::getRegexType, entity.getRegexType().substring(1));
            } else {
                queryWrapper.lambda().like(CommonValidator::getRegexType, entity.getRegexType());
            }
        }
        queryWrapper.orderByDesc("update_time", "id");
    }

    @GetMapping("/allList")
    protected Mono<ResultBody> allList() {
        return Mono.just(ResultBody.ok(service.list()));
    }
}
